<%@ page language="java" contentType="text/html; charset=UTF-8"  isErrorPage="true"
    pageEncoding="UTF-8"%>
<div class="layuimini-main">

    <div class="layui-form layuimini-form" lay-filter="healthKnowledgeedit">
        <input type="hidden" name="id" value="">
        <div class="layui-form-item">
            <label class="layui-form-label required">健康知识标题</label>
            <div class="layui-input-block">
                <input type="text" name="title" lay-verify="required" lay-reqtext="健康知识标题不能为空" 
                placeholder="请输入健康知识标题" value="" class="layui-input">
            </div>
        </div>
        
          <div class="layui-form-item">
    <div class="layui-input-block">
      <button type="button" class="layui-btn" id="upload">
        <i class="layui-icon">&#xe67c;</i>上传首页图片
        <input type="hidden" value="" id="images"  name="images">
      </button>
      <br><br>
      <div style="width:200px;height:200px;border:3px solid #0099CC;border-radius: 5px;padding: 3px;">
        <img style="max-width: 200px;max-height:200px;" id="preview">
      </div>
    </div>
  </div>
  
  <div class="layui-form-item layui-form-text">
           <label class="layui-form-label required">简介</label>
           <div class="layui-input-block">
               <textarea name="intro" class="layui-textarea" placeholder="请输入简介"></textarea>
           </div>
        </div>
        
        
        <div class="layui-form-item">
            <label class="layui-form-label required">健康知识内容</label>
            <div class="layui-input-block">
                <!-- <input type="text" name="content" lay-verify="required" lay-reqtext="健康知识内容不能为空" 
                placeholder="请输入健康知识内容" value="" class="layui-input"> -->
                <textarea id="lay_edit" lay-verify="content" name="content" class="layui-textarea" placeholder="请输入健康知识内容"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">创建时间</label>
            <div class="layui-input-block">
                <input type="text" name="createDate" id="date" lay-verify="required" lay-reqtext="创建时间不能为空" 
                placeholder="请输入创建时间" value="" class="layui-input">
            </div>
        </div>
       

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </div>
</div>
<script src="/PC_educationms/js/jquery-3.3.1.js"></script>
<script>
    layui.use(['form', 'table', 'upload', 'element', 'laydate', 'layedit'], function () {
        var form = layui.form,
            layer = layui.layer,
            table = layui.table,
            upload = layui.upload,
            element = layui.element,
            laydate = layui.laydate,
            layedit = layui.layedit,
            $ = layui.$;
        
        laydate.render({
        	elem:'#date',
        	type:'datetime',
        	trigger:'click'
        })

        // 上传首页图片
     $("#preview").attr("src",parent.images);
     console.log(parent.blogType);
        upload.render({
      elem: '#upload',
      url: '/PC_educationms/Upload',
      auto: true,//选择文件后不自动上传
      bindAction: '#commit',
      //上传前的回调
      before: function () {
        this.data = {
          name: $('input[name="name"]').val()
        }
      },
      //选择文件后的回调
      choose: function (obj) {
        obj.preview(function (index, file, result) {
          $('#preview').attr('src', result);
        })
      },
      //操作成功的回调
      done: function (res, index, upload) {
        var code = res.code === 0 ? 1 : 2;
        console.log(code);
        console.log(res);
        console.log(index);
        console.log(upload);
        var images = document.getElementById("images");
        images.value=res.data.src;
        /* layer.alert(res.msg, {icon: code}, function () {
          parent.window.location.reload();
        }) */
      },
      //上传错误回调
      error: function (index, upload) {
        layer.alert('上传失败！' + index);
      }
    });
        
        
        // 富文本渲染
        var layedit = layui.layedit;
        layedit.set({	//设置图片接口
            uploadImage: {
                url: '/PC_educationms/Upload', //接口url
                type: 'post'
            }
        });
        console.log(parent.contents)
        $("#lay_edit").val(parent.contents)
        //创建一个编辑器，lay_edit为textarea标签的id值
        var index = layedit.build('lay_edit',{
            width:400,
            height: 400    //设置编辑器的高度
        }); 
        //提交时把值同步到文本域中
        form.verify({
            //content富文本域中的lay-verify值
            content: function(value) {
                return layedit.sync(index);
            }
        });
        
        
        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        form.render();

        // 当前弹出层，防止ID被覆盖
        var parentIndex = layer.index;

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var index = layer.alert("确定修改心理健康知识！", {
                title: '提交信息'
            }, function () {
                 console.log(data);
                 $.ajax({
                	 url:"/PC_educationms/healthKnowledge/updateHealthKnowledge",
                	 data:JSON.stringify(data.field),
                	 type:"POST",
                	 dataType:"json",
                     contentType:"application/json",
                	 success:function(data){
                		 console.log(data);
                   		 // data = JSON.parse(data);
                   		 if(data.msg == "ok") {
                   			// 关闭弹出层
                            layer.close(index);
                            layer.close(parentIndex);
                            $(".layui-laypage-btn")[0].click();
                   		 }else{
                             layer.alert('修改失败！请输入正确信息。'+(data.handle!=undefined?data.handle:""))
                     	}
                	 }
                 })
            });
            return false;
        });

    });
</script>